home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / editor / EdAEHTMLAttributes.js < prev    next >
Encoding:
JavaScript  |  2001-06-12  |  10.5 KB  |  333 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben "Count XULula" Goodger
  22.  */
  23.  
  24. function BuildHTMLAttributeNameList()
  25. {
  26.   ClearMenulist(dialog.AddHTMLAttributeNameInput);
  27.   
  28.   var elementName = gElement.localName.toLowerCase();
  29.   var attNames = gHTMLAttr[elementName];
  30.  
  31.   if (attNames && attNames.length)
  32.   {
  33.     var forceOneChar = false;
  34.     var forceInteger = false;
  35.  
  36.     for (var i = 0; i < attNames.length; i++)
  37.     {
  38.       var name = attNames[i];
  39.       if (name == "_core")
  40.       {
  41.         // Signal to append the common 'core' attributes.
  42.         // Note: These currently don't have any filtering
  43.         for (var j = 0; j < gCoreHTMLAttr.length; j++)
  44.           AppendStringToMenulist(dialog.AddHTMLAttributeNameInput, gCoreHTMLAttr[j]);
  45.  
  46.       }
  47.       else if (name == "-")
  48.       {
  49.         // Signal for separator
  50.         var popup = dialog.AddHTMLAttributeNameInput.firstChild;
  51.         if (popup)
  52.         {
  53.           sep = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuseparator");
  54.           if (sep)
  55.             popup.appendChild(sep);
  56.         }        
  57.       }
  58.       else
  59.       {
  60.         // Get information about value filtering
  61.         forceOneChar = name.indexOf("!") >= 0;
  62.         forceInteger = name.indexOf("#") >= 0;
  63.         forceIntOrPercent = name.indexOf("%") >= 0;
  64.         //var required = name.indexOf("$") >= 0;
  65.  
  66.         // Strip flag characters ("_" is used when attribute name is reserved JS word)
  67.         name = name.replace(/[!#%$_]/g, "");
  68.  
  69.         var menuitem = AppendStringToMenulist(dialog.AddHTMLAttributeNameInput, name);
  70.         if (menuitem)
  71.         {
  72.           // Signify "required" attributes by special style
  73.           //TODO: Don't do this until next version, when we add
  74.           //      explanatory text and an 'Autofill Required Attributes' button
  75.           //if (required)
  76.           //  menuitem.setAttribute("class", "menuitem-highlight-1");
  77.  
  78.           // Set flags to filter value input
  79.           menuitem.setAttribute("forceOneChar", forceOneChar ? "true" : "");
  80.           menuitem.setAttribute("forceInteger", forceInteger ? "true" : "");
  81.           menuitem.setAttribute("forceIntOrPercent", forceIntOrPercent ? "true" : "");
  82.         }
  83.       }
  84.     }
  85.   }
  86.  
  87.   // Always start with empty input fields
  88.   ClearHTMLInputWidgets();
  89. }
  90.  
  91. // build attribute list in tree form from element attributes
  92. function BuildHTMLAttributeTable()
  93. {
  94.   var nodeMap = gElement.attributes;
  95.   var i;
  96.   if (nodeMap.length > 0) 
  97.   {
  98.     var added = false;
  99.     for(i = 0; i < nodeMap.length; i++)
  100.     {
  101.       if ( CheckAttributeNameSimilarity( nodeMap[i].nodeName, HTMLAttrs ) ||
  102.           IsEventHandler( nodeMap[i].nodeName ) ||
  103.           TrimString( nodeMap[i].nodeName.toLowerCase() ) == "style" ) {
  104.         continue;   // repeated or non-HTML attribute, ignore this one and go to next
  105.       }
  106.       var name  = nodeMap[i].nodeName.toLowerCase();
  107.       var value = gElement.getAttribute ( nodeMap[i].nodeName );
  108.       if ( name.indexOf("_moz") != 0 &&
  109.            AddTreeItem(name, value, "HTMLAList", HTMLAttrs) )
  110.         added = true;
  111.     }
  112.  
  113.     if (added)
  114.       SelectHTMLTree(0);
  115.   }
  116. }
  117.  
  118. // add or select an attribute in the tree widget
  119. function onChangeHTMLAttribute()
  120. {
  121.   var name  = TrimString(dialog.AddHTMLAttributeNameInput.value);
  122.   if (!name)
  123.     return;
  124.  
  125.   var value = TrimString(dialog.AddHTMLAttributeValueInput.value);
  126.  
  127.   // First try to update existing attribute
  128.   // If not found, add new attribute
  129.   if ( !UpdateExistingAttribute( name, value, "HTMLAList" ) )
  130.     AddTreeItem ( name, value, "HTMLAList", HTMLAttrs );
  131. }
  132.  
  133. function ClearHTMLInputWidgets()
  134. {
  135.   dialog.AddHTMLAttributeTree.clearItemSelection();
  136.   dialog.AddHTMLAttributeNameInput.value ="";
  137.   dialog.AddHTMLAttributeValueInput.value = "";
  138.   dialog.AddHTMLAttributeNameInput.inputField.focus();
  139. }
  140.  
  141. function onSelectHTMLTreeItem()
  142. {
  143.   if (!gDoOnSelectTree)
  144.     return;
  145.  
  146.   var tree = dialog.AddHTMLAttributeTree;
  147.   if (tree && tree.selectedItems && tree.selectedItems.length)
  148.   {
  149.     var inputName = TrimString(dialog.AddHTMLAttributeNameInput.value).toLowerCase();
  150.     var selectedName = tree.selectedItems[0].firstChild.firstChild.getAttribute("label");
  151.  
  152.     if (inputName == selectedName)
  153.     {
  154.       // Already editing selected name - just update the value input
  155.       dialog.AddHTMLAttributeValueInput.value =  GetTreeItemValueStr(tree.selectedItems[0]);
  156.     }
  157.     else
  158.     {
  159.       dialog.AddHTMLAttributeNameInput.value =  selectedName;
  160.  
  161.       // Change value input based on new selected name
  162.       onInputHTMLAttributeName();
  163.     }
  164.   }
  165. }
  166.  
  167. function onInputHTMLAttributeName()
  168. {
  169.   var attName = TrimString(dialog.AddHTMLAttributeNameInput.value).toLowerCase();
  170.  
  171.   // Clear value widget, but prevent triggering update in tree
  172.   gUpdateTreeValue = false;
  173.   dialog.AddHTMLAttributeValueInput.value = "";
  174.   gUpdateTreeValue = true; 
  175.  
  176.   if (attName)
  177.   {
  178.     // Get value list for current attribute name
  179.     var valueList = gHTMLAttr[gElement.localName.toLowerCase() + "_" + attName];
  180.  
  181.     // Index to which widget we were using to edit the value
  182.     var deckIndex = dialog.AddHTMLAttributeValueDeck.getAttribute("index");
  183.     var newValue = "";
  184.     var listLen = 0;
  185.     if (valueList)
  186.     {
  187.       listLen = valueList.length;
  188.       if (listLen > 0)
  189.         newValue = valueList[0];
  190.  
  191.       // Note: For case where "value list" is actually just 
  192.       // one (default) item, don't use menulist for that
  193.       if (listLen > 1)
  194.       {
  195.         ClearMenulist(dialog.AddHTMLAttributeValueMenulist);
  196.  
  197.         if (deckIndex != "1")
  198.         {
  199.           // Switch to using editable menulist
  200.           dialog.AddHTMLAttributeValueInput = dialog.AddHTMLAttributeValueMenulist;
  201.           dialog.AddHTMLAttributeValueDeck.setAttribute("index", "1");
  202.         }
  203.         // Rebuild the list
  204.         for (var i = 0; i < listLen; i++)
  205.           AppendStringToMenulist(dialog.AddHTMLAttributeValueMenulist, valueList[i]);
  206.       }
  207.     }
  208.     
  209.     if (listLen <= 1 && deckIndex != "0")
  210.     {
  211.       // No list: Use textbox for input instead
  212.       dialog.AddHTMLAttributeValueInput = dialog.AddHTMLAttributeValueTextbox;
  213.       dialog.AddHTMLAttributeValueDeck.setAttribute("index", "0");
  214.     }
  215.  
  216.     // If attribute already exists in tree, use associated value,
  217.     //  else use default found above
  218.     var existingValue = GetAndSelectExistingAttributeValue(attName, "HTMLAList");
  219.     if (existingValue)
  220.       newValue = existingValue;
  221.       
  222.     dialog.AddHTMLAttributeValueInput.value = newValue;
  223.   }
  224. }
  225.  
  226. function onInputHTMLAttributeValue()
  227. {
  228.   if (!gUpdateTreeValue)
  229.     return;
  230.  
  231.   // Trim spaces only from left since we must allow spaces within the string
  232.   //  (we always reset the input field's value below)
  233.   var value = TrimStringLeft(dialog.AddHTMLAttributeValueInput.value);
  234.   if (value)
  235.   {
  236.     // Do value filtering based on type of attribute
  237.     // (Do not use "LimitStringLength()" and "forceInteger()"
  238.     //  to avoid multiple reseting of input's value and flickering)
  239.     var selectedItem = dialog.AddHTMLAttributeNameInput.selectedItem;
  240.     if (selectedItem)
  241.     {
  242.       if ( selectedItem.getAttribute("forceOneChar") == "true" &&
  243.            value.length > 1 )
  244.         value = value.slice(0, 1);
  245.  
  246.       if ( selectedItem.getAttribute("forceIntOrPercent") == "true" )
  247.       {
  248.         // Allow integer with optional "%" as last character
  249.         var percent = TrimStringRight(value).slice(-1);
  250.         value = value.replace(/\D+/g,"");
  251.         if (percent == "%")
  252.           value += percent;
  253.       }
  254.       else if ( selectedItem.getAttribute("forceInteger") == "true" )
  255.       {
  256.         value = value.replace(/\D+/g,"");
  257.       }
  258.  
  259.       // Update once only if it changed
  260.       if ( value != dialog.AddHTMLAttributeValueInput.value )
  261.         dialog.AddHTMLAttributeValueInput.value = value;
  262.     }
  263.   }
  264.  
  265.   // Always update value in the tree list
  266.   UpdateExistingAttribute( dialog.AddHTMLAttributeNameInput.value, value, "HTMLAList" );
  267. }
  268.  
  269. function editHTMLAttributeValue(targetCell)
  270. {
  271.   if (IsNotTreeHeader(targetCell))
  272.     dialog.AddHTMLAttributeValueInput.inputField.select();
  273. }
  274.  
  275.  
  276. // update the object with added and removed attributes
  277. function UpdateHTMLAttributes()
  278. {
  279.   var HTMLAList = document.getElementById("HTMLAList");
  280.   var i;
  281.  
  282.   // remove removed attributes
  283.   for( i = 0; i < HTMLRAttrs.length; i++ )
  284.   {
  285.     var name = HTMLRAttrs[i];
  286.  
  287.     // We can't use getAttribute to figure out if attribute already
  288.     //  exists for attributes that don't require a value
  289.     // This gets the attribute NODE from the attributes NamedNodeMap
  290.     if (gElement.attributes.getNamedItem(name))
  291.       gElement.removeAttribute(name);
  292.   }
  293.  
  294.   // Set added or changed attributes
  295.   for( i = 0; i < HTMLAList.childNodes.length; i++)
  296.   {
  297.     var item = HTMLAList.childNodes[i];
  298.     gElement.setAttribute( GetTreeItemAttributeStr(item), GetTreeItemValueStr(item) );
  299.   }
  300. }
  301.  
  302. function RemoveHTMLAttribute()
  303. {
  304.   var treechildren = dialog.AddHTMLAttributeTree.lastChild;
  305.  
  306.   // We only allow 1 selected item
  307.   if (dialog.AddHTMLAttributeTree.selectedItems.length)
  308.   {
  309.     var item = dialog.AddHTMLAttributeTree.selectedItems[0];
  310.     var attr = GetTreeItemAttributeStr(item);
  311.  
  312.     // remove the item from the attribute array
  313.     HTMLRAttrs[HTMLRAttrs.length] = attr;
  314.     RemoveNameFromAttArray(attr, HTMLAttrs);
  315.  
  316.     // Remove the item from the tree
  317.     treechildren.removeChild(item);
  318.  
  319.     // Clear inputs and selected item in tree
  320.     ClearHTMLInputWidgets();
  321.   }
  322. }
  323.  
  324. function SelectHTMLTree( index )
  325. {
  326.  
  327.   gDoOnSelectTree = false;
  328.   try {
  329.     dialog.AddHTMLAttributeTree.selectedIndex = index;
  330.   } catch (e) {}
  331.   gDoOnSelectTree = true;
  332. }
  333.